var page = 1;//页数
var total = 0;//总条数
var totalPage = 0;//总页数

$(function () {

    init(1);

    $("#pre").click(function () {
        if (page == parseInt(total/10)+1){$("#next").attr("disabled",false)};
        page = page-1;
        if (page<=0){page=1};
        init(page);
    })

    $("#next").click(function () {
        if (page == 1){$("#pre").attr("disabled",false)};
        page = page+1;
        if (page > (total/10)+1){page = (total/10)+1};
        init(page);
    });

})

function init(index){
    page=index;
    var html = `<tr>
                <th>序号</th>
                <th>联络站名称</th>
                <th>联络站地址</th>
                <th>联系人</th>
                <th>联系电话</th>
                <th>接待时间</th>
                <th>操作区</th>
            </tr>`; //表格标题
    $.ajax({
        url:'./js/digitalPlatform/interfacelocation.json',
        type: 'GET',
        dataType: 'JSON',
        success: function (data) {
            var llzData = data.results;
            total = llzData.length;
            totalPage = parseInt(total / 10 + 1);
            $.each(llzData, function (index, val) {
                if (index >= (page - 1) * 10 && index < 10 * page) {
                    html += `<tr>
                <td class="align_center" width="100">${index + 1}</td>
                <td class="align_center" width="400">${val.llzName}</td>
                <td class="align_center" width="600">${val.address}</td>
                <td class="align_center" width="200">${val.principal}</td>
                <td class="align_center" width="250">${val.phone}</td>
                <td class="align_center" width="400">${val.date}</td>
                <td class="align_center" width="100"><a href="javascript:;" onclick="info('${val.llzName}')">查看</a></td>
            </tr>`
                }
            })
            // console.log(page);
            $("#llz").html(html);
            if (page == 1) {
                $("#pre").attr("disabled", true)
            } else {
                $("#pre").attr("disabled", false)
            }
            ;
            if (page == parseInt(total / 10) + 1) {
                $("#next").attr("disabled", true)
            } else {
                $("#next").attr("disabled", false)
            }
            ;
            pageLoad();
            // console.log(parseInt(total/15)+1)
        }
    })
}

function info(item) {
    // console.log(item);
    $.ajax({
        url:'./js/digitalPlatform/interfacelocation.json',
        type: 'GET',
        dataType: 'JSON',
        success: function (data) {
            var llzData = data.results;
            $.each(llzData, function (index, val) {
                if (item == val.llzName) {
                    //tab页切换问题
                    $("#province").css("display", "none");
                    provTab();

                    //代表信息匹配加载
                    loadResp(val.resp);

                    if (val.image && val.image.length >0){
                        $("#llzImage").html("<img src="+val.image+" />")
                    }
                    // console.log(val);
                    $(".llzName").html(val.llzName);
                    $("#address").html(val.address);
                    $("#principal").html(val.principal);
                    $("#phone").html(val.phone);
                    $("#date").html(val.date);
                    $("#cover").toggle();
                    $(".float-1").css("display", "block");
                }
            })
        }
    })
}

//代表信息加载
function loadResp(resp){
    // console.log(resp)
    let sty = 10;//控制tab页切换css样式
    //省级代表加载
    if (undefined!=resp && ""!=resp){
        if (resp.level1 && resp.level1.length >0){
            sty=1;
            $("#tab1").css("display","block");
            $("#province").css("display","none");
            var prov = resp.level1;
            let province = "";
            for (let item of prov) {
                // console.log(image);
                var pHeadicon=item.pHeadicon;
                if (pHeadicon!=""&&pHeadicon!=undefined){

                }else {
                    pHeadicon="css/images/db_icon.png"
                }
                province += ` <div class="db_info" item-id=${item.id} onclick="dbInfo(this)"><img src="`+pHeadicon+`" width="60" />
            <p>${item.pName}</p>
            </div>`
            }
            $("#province").html(province);
            // $.parser.parse("#province");
        }else{
            $("#tab1").css("display","none");
        }
        //市级代表加载
        if (resp.level2  && resp.level2.length >0){
            if (sty>2){sty=2}
            $("#tab2").css("display","block");
            $("#city").css("display","none");
            var city = resp.level2;
            let ci = "";
            for (let item of city) {
                var pHeadicon=item.pHeadicon;
                if (pHeadicon!=""&&pHeadicon!=undefined){

                }else {
                    pHeadicon="css/images/db_icon.png"
                }
                // console.log(image);
                ci += ` <div class="db_info" item-id=${item.id} onclick="dbInfo(this)"><img src="`+pHeadicon+`" width="60" />
            <p>${item.pName}</p>
            </div>`
            }
            $("#city").html(ci);
            // $.parser.parse("#city");
        }else{
            $("#tab2").css("display","none");
            // $("#city").html('<div class="no-data" style="padding-top:15px;">暂无我们的代表</div>');
        }

        //乡级代表加载
        if (resp.level3  && resp.level3.length >0){
            if (sty>3){sty=3}
            $("#tab3").css("display","block");
            $("#county").css("display","none");
            var county = resp.level3;
            let co = "";
            for (let item of county) {
                var pHeadicon=item.pHeadicon;
                if (pHeadicon!=""&&pHeadicon!=undefined){

                }else {
                    pHeadicon="css/images/db_icon.png"
                }
                // console.log(image);
                co += ` <div class="db_info" item-id=${item.id} onclick="dbInfo(this)"><img src="`+pHeadicon+`" width="60" />
            <p>${item.pName}</p>
            </div>`
            }
            $("#county").html(co);
            // $.parser.parse("#county");
        }else{
            $("#tab3").css("display","none");
            // $("#county").html('<div class="no-data" style="padding-top:15px;">暂无我们的代表</div>');
        }

        //镇级代表加载
        if (resp.level4  && resp.level4.length >0){
            if (sty>4){sty=4}
            $("#tab4").css("display","block");
            $("#village").css("display","none");
            var village = resp.level4;
            let vill = "";
            for (let item of village) {
                var pHeadicon=item.pHeadicon;
                if (pHeadicon!=""&&pHeadicon!=undefined){

                }else {
                    pHeadicon="css/images/db_icon.png"
                }
                // console.log(image);
                vill += ` <div class="db_info" item-id=${item.id} onclick="dbInfo(this)"><img src="`+pHeadicon+`" width="60" />
            <p>${item.pName}</p>
            </div>`
            }
            $("#village").html(vill);
            // $.parser.parse("#village");
        }else{
            $("#tab4").css("display","none");
            // $("#village").html('<div class="no-data" style="padding-top:15px;">暂无我们的代表</div>');
        }

        // 控制tab页css样式
        if (sty==1){
            provTab();
        } else if (sty ==2){
            cityTab();
        }else if (sty == 3){
            countyTab();
        }else {
            villageTab();
        }
    }
}

//加载代表基本信息详情
function dbInfo(item) {
    var itemId=item.getAttribute("item-id");
    var urldb='代表信息.html?id='+itemId;
    window.open(urldb);
}


//翻页功能
function pageLoad() {
    var i = 1;
    i = parseInt(i);
    var item="";
    if (totalPage <= 3 ) {//总页数小于五页，则加载所有页

        for (i; i <= totalPage; i++) {
            if (i == page) {
                item += "<span class='disabled'>"+i+"</span>";
            }else{
                item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
            }
        };
        $('#pageBtn').html(item);
        return;
    }else if (totalPage > 3) {//总页数大于五页，则加载五页
        if (page < 3) {//当前页小于5，加载1-5页
            for (i; i <= 3; i++) {
                if (i == page) {
                    item += "<span class='disabled'>"+i+"</span>";
                }else{
                    item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                }
            };
            if (page <= totalPage-2) {//最后一页追加“...”代表省略的页
                item += "<span> . . . </span>";
                item += "<span class='show' onclick='init("+totalPage+")'>"+totalPage+"</span>"
            }
            $('#pageBtn').html(item);
            return;
        }else if (page >= 3) {//当前页大于5页
            for (i; i <= 1; i++) {//1,2页码始终显示
                item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
            }
            item += "<span> . . . </span>";//2页码后面用...代替部分未显示的页码
            if (page+1 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else if (page == totalPage) {//当前页数等于总页数则是最后一页页码显示在最后
                for(i = page-2; i <= totalPage; i++){//...后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else if (page+2 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else{//当前页小于总页数，则最后一页后面跟...
                for(i = page-1; i <= page+1; i++){//page+1页后面...
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
                item += "<span> . . . </span>";
                item += "<span class='show' onclick='init("+totalPage+")'>"+totalPage+"</span>"
            }
            $('#pageBtn').html(item);
            return;
        }
        // console.log(item);
    }

}
//tab页切换方法
function provTab(){
    $("#province").toggle();
    $("#tab1").attr("class","cur");
    $("#tab2").attr("class","unselect");
    $("#tab3").attr("class","unselect");
    $("#tab4").attr("class","unselect");
    $("#city").css("display","none");
    $("#county").css("display","none");
    $("#village").css("display","none");
}

function cityTab(){
    $("#city").toggle();
    $("#tab2").attr("class","cur");
    $("#tab1").attr("class","unselect");
    $("#tab3").attr("class","unselect");
    $("#tab4").attr("class","unselect");
    $("#province").css("display","none");
    $("#county").css("display","none");
    $("#village").css("display","none");
}

function countyTab(){
    $("#county").toggle();
    $("#tab3").attr("class","cur");
    $("#tab2").attr("class","unselect");
    $("#tab1").attr("class","unselect");
    $("#tab4").attr("class","unselect");
    $("#city").css("display","none");
    $("#province").css("display","none");
    $("#village").css("display","none");
}

function villageTab(){
    $("#village").toggle();
    $("#tab4").attr("class","cur");
    $("#tab2").attr("class","unselect");
    $("#tab3").attr("class","unselect");
    $("#tab1").attr("class","unselect");
    $("#city").css("display","none");
    $("#county").css("display","none");
    $("#province").css("display","none");
}

//取消键
function close1() {
    $("#cover").css("display","none");
    $(".float-1").css("display","none");
}






